package com.example.service.impl;

import com.example.dao.IAccountDao;
import com.example.domain.Account;
import com.example.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 账号业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;

    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    public void deleteAccount(Integer accountId) {
        accountDao.deleteAccount(accountId);
    }

    public void batchSave(List<Account> ats) {
        accountDao.batchSave(ats);
    }

    @Transactional
    public void account(String sourceName, String targetName, Float money) {
        //1.获取转出账户
        Account source = accountDao.findAccoutByName(sourceName);
        //2.获取转入账户
        Account target = accountDao.findAccoutByName(targetName);
        //3.转出账户金额变化
        source.setMoney(source.getMoney()-money);
        //4.转入账户金额变化
        target.setMoney(target.getMoney()+money);
        //5.转出账户更新
        accountDao.updateAccount(source);

        int error = 1/0;
        //6.转入账户更新
        accountDao.updateAccount(target);
    }
}
